package cn.wit.summit.chwan.ui.activity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.adapters.TreviFountainListAdapter;
import cn.wit.summit.chwan.interfaces.AdapterInterface;
import cn.wit.summit.chwan.models.response.GetTreviListResponse;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.ui.base.MyActivity;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.widgets.refreshLayout.SmartRefreshLayout;
import cn.wit.summit.chwan.widgets.refreshLayout.api.RefreshLayout;
import cn.wit.summit.chwan.widgets.refreshLayout.listener.OnLoadMoreListener;
import cn.wit.summit.chwan.widgets.refreshLayout.listener.OnRefreshListener;
import retrofit2.Retrofit;

/**
 * Created by Alan on 2018/7/31.
 */

public class TreviFountainListActivity extends MyActivity implements AdapterInterface {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private int pn = 1;
    private TreviFountainListAdapter adapter;
    private LinearLayoutManager layoutManager;
    private List<TreviFountainListAdapter.ViewBean> viewBeans = new ArrayList<>();

    private RetrofitManager retrofitManager;
    private Retrofit retrofitLogin;
    private ApiUtil apiLogin;

    private TextView imTopRight;
    private int type = 1; //1全部  2未解决   3我参与

    private boolean isFormCreateTrevi = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trevi_fountain_list);
        ButterKnife.bind(this);

        isFormCreateTrevi = getIntent().getBooleanExtra("isFormCreateTrevi", false);

        retrofitManager = RetrofitManager.getInstance(mContext);
        retrofitLogin = retrofitManager.getLoginRetrofit();
        apiLogin = retrofitLogin.create(ApiUtil.class);

        setBarTitleTx(R.string.reward_pool_title_str);
        imTopRight = setBarRightTx("筛选");
        imTopRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPop();
            }
        });

        initListView();
    }

    private void initListView() {
        adapter = new TreviFountainListAdapter(mAty, viewBeans, isFormCreateTrevi, this);
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.setFocusable(false);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                pn = 1;
                getListData();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                getListData();
            }
        });

        showSkeleton();
        pn = 1;
        getListData();
    }

    @Override
    public void reGetData() {
        super.reGetData();
        viewBeans.clear();
        adapter.notifyDataSetChanged();
        pn = 1;
        getListData();
    }

    private void getListData() {
        try {
            int uid = AccountUtil.getInstance().getUid();
            apiLogin.getTreviList(uid, pn, type).enqueue(new MyCallback<ResponseBase<List<GetTreviListResponse>>>() {
                @Override
                public void onSuccess(ResponseBase<List<GetTreviListResponse>> model) {
                    List<GetTreviListResponse> data = model.getData_info();
                    hideSkeleton();
                    setUiView(data);
                }

                @Override
                public void onFailure(int code, String msg) {
                    hideSkeleton();
                    refreshLayout.finishRefresh(false);
                    refreshLayout.finishLoadMore(false);
                    refreshLayout.finishRefresh();
                    showToast(msg);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setUiView(List<GetTreviListResponse> data) {
        if (data == null) {
            if (pn == 1) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
            }
            if (data.size()==0) {
                showFailedLoding("暂无数据");
            }
            return;
        }

        if (pn==1) {
            viewBeans.clear();
            viewBeans.add(new TreviFountainListAdapter.ViewBean(TreviFountainListAdapter.ViewType.TYPE_TOP_TITLE, ""));
        }
        for (int i=0;i<data.size();i++) {
            viewBeans.add(new TreviFountainListAdapter.ViewBean(TreviFountainListAdapter.ViewType.TYPE_ITEM_LIST, data.get(i)));
        }

        if (data.size() <= 0) {
            if (pn == 1) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
            }
        } else {
            if (pn == 1) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }
            pn++;
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onAdapterItemClick(View view, int position) {

    }

    private void showPop() {
        View popupView = this.getLayoutInflater().inflate(R.layout.trevi_list_top_popwin, null);
        TextView popListAllTx = popupView.findViewById(R.id.pop_list_all_tx);
        ImageView line1 = popupView.findViewById(R.id.line1);
        TextView popListUnTx = popupView.findViewById(R.id.pop_list_un_tx);
        ImageView line2 = popupView.findViewById(R.id.line2);
        TextView popListJoinTx = popupView.findViewById(R.id.pop_list_join_tx);

        // 创建PopupWindow对象，指定宽度和高度
        final PopupWindow window = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT
                , ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置动画
//        window.setAnimationStyle(R.style.popup_window_anim);
        // 设置背景颜色
        window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        // 设置可以获取焦点
        window.setFocusable(true);
        // 设置可以触摸弹出框以外的区域
        window.setOutsideTouchable(true);
        // 更新popupwindow的状态
        window.update();
        // 以下拉的方式显示，并且可以设置显示的位置
        window.showAsDropDown(imTopRight, 0, 0);

        if (type==1) {
            popListAllTx.setVisibility(View.GONE);
            line1.setVisibility(View.GONE);
        } else if (type==2) {
            popListUnTx.setVisibility(View.GONE);
            line2.setVisibility(View.GONE);
        } else if (type==3) {
            popListJoinTx.setVisibility(View.GONE);
            line2.setVisibility(View.GONE);
        }

        popListAllTx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
                type = 1;
                pn = 1;
                getListData();
            }
        });

        popListUnTx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
                type = 2;
                pn = 1;
                getListData();
            }
        });

        popListJoinTx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
                type = 3;
                pn = 1;
                getListData();
            }
        });
    }

    @Override
    protected int getSkeletonLayoutId() {
        return R.layout.activity_trevi_fountain_list_skeleton;
    }
}
